package scala

import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.{LabeledPoint, LinearRegressionWithSGD}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * 项目名称:spark-learn<br>
  * 包名:scala<br>
  * 用于:spark-learn<br>
  * 创建时间:2019年03月16日<br>
  * 更新时间:2019年03月16日<br>
  *
  * @author :lds（创建人）<br>
  * @version :v1.0（版本号）<br>
  * @since jdk1.8
  */
object LinearRegression2 {


  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local").setAppName("CollaborativeFilter")
    val sc = new SparkContext(conf)
    val data = sc.textFile("conf\\lr.txt")
    val parseData = data.map {
      line =>
        val sparts = line.split("|")
        LabeledPoint(sparts(0).toDouble, Vectors.dense(sparts(1).split(",").map(_.toDouble)))
    }.cache()
    val model = LinearRegressionWithSGD.train(parseData,200,0.01)
    val predictation = model.predict(parseData.map(_.features))
    predictation.foreach(println)
    //TODO 报错，需要检查变量分割
    println(model.predict(Vectors.dense(0,1)))
  }

}
